Using maps and a key to give a unique URL everytime
1
prajwal_agarwal's avatar

Visit my Youtube channel for
-> videos about developing intuition for difficult coding concepts
-> using intuition for solving some good coding questions

https://www.youtube.com/channel/UC-3Ceh2I6aDerx98d5r2rsQ

Code:

class Solution {
public:
    unordered_map<string, string> decode_map;
    int key = 0;
    
    // Encodes a URL to a shortened URL.
    string encode(string longUrl) {
        string encoded = "http://tinyurl.com/" + to_string(key++);
        decode_map[encoded] = longUrl;
        return encoded;
    }

    // Decodes a shortened URL to its original URL.
    string decode(string shortUrl) {
        return decode_map[shortUrl];
    }
};

// Your Solution object will be instantiated and called as such:
// Solution solution;
// solution.decode(solution.encode(url));